<?php
namespace Home\Controller;
use Think\Controller;
class CenterController extends HController {
   public function index() {
    $uid = $_SESSION['qzone']['home']['id'];
    $user_model = M('User');
    $say_model = M('Say');
    $say_good_model = M('Say_good');
    $say_comment_model = M('Say_comment');
    $say_comment_hfmodel = M('Say_comment_huif');
    $say_photo_model = M('Say_photo');
    $log_model = M('Log');
    $log_good_model = M('Log_good');
    $log_comment_model = M('Log_comment');
    $log_comment_hfmodel = M('Log_comment_huif');
    $album_model = M('Album');
    $friend_model = M('Friends');
    // $album_comment_model = M('Album_comment');
    $album_photo_model = M('Album_photo');
    //获取当前用户好友id
    $friend_list = $friend_model->field('who_id')->where(array('uid'=>$uid,'state'=>1))->select();
    foreach ($friend_list as $key => $val) {
        $rs = $user_model->where(array('id'=>$val['who_id'],'state'=>0))->find();
        if(!empty($rs)){
            $friend_id[] = $val['who_id'];
        }        
    }
    $friend_id[] = $_SESSION['qzone']['home']['id'];

    //生成按好友id查询条件
    $where['uid'] = array('in',$friend_id);

    //获取好友说说动态列表
    $say_list = $say_model->where($where)->order('ctime desc,id')->select();
    //获取每条说说动态的图片及评论点赞信息
    foreach ($say_list as $key => $val) {
        $id = $val['id'];
        $uid = $val['uid'];
        //获取每条说说的用户信息
        $user = $user_model->find($uid);
        $say_list[$key]['user'] = $user;
        //获取每条说说的图片列表
        $say_photo_list = $say_photo_model->where(array('say_id'=>$id))->order('ctime desc,id')->select();
        //获取每条说说的点赞数量
        $say_good_num = $say_good_model->where(array('say_id'=>$id))->count();
        //获取每条说说的评论数量
        $say_comment_num = $say_comment_model->where(array('who_id'=>$id))->count();
        //获取每条说说的评论列表
        $say_comment_list = $say_comment_model->where(array('who_id'=>$id))->order('ctime asc,id')->select();
        //获取每条评论的用户信息及回复信息
        foreach ($say_comment_list as $k => $value) {
            $uid = $value['uid'];
            $user = $user_model->find($uid);
            $say_comment_list[$k]['user'] = $user;
            //获取每条评论回复列表
            $say_hflist = $say_comment_hfmodel->where(array('com_id'=>$value['id']))->select();
            //获取每条回复用户信息
            foreach ($say_hflist as $k1 => $v1) {
                $user1 = $user_model->find($v1['uid']);
                $user2 = $user_model->find($v1['hf_id']);
                $say_hflist[$k1]['user1'] = $user1;
                $say_hflist[$k1]['user2'] = $user2;
            }
            $say_comment_list[$k]['hflist'] = $say_hflist;
        }
        $say_list[$key]['title'] = '';
        $say_list[$key]['goodnum'] = $say_good_num;
        $say_list[$key]['comnum'] = $say_comment_num;
        $say_list[$key]['photo'] = $say_photo_list;
        $say_list[$key]['comment'] = $say_comment_list;
        $say_list[$key]['type'] = 'Say';
    }

    //获取好友日志动态列表
    $log_list = $log_model->where($where)->order('ctime desc,id')->select();
    foreach ($log_list as $key => $val) {
        $id = $val['id'];
        $uid = $val['uid'];
        //获取每条日志的用户信息
        $user = $user_model->find($uid);
        $log_list[$key]['user'] = $user;
        //获取每条日志的点赞数量
        $log_good_num = $log_good_model->where(array('log_id'=>$id))->count();
        //获取每条日志的评论数量
        $log_comment_num = $log_comment_model->where(array('who_id'=>$id))->count();
        //获取每条日志的评论列表
        $log_comment_list = $log_comment_model->where(array('who_id'=>$id))->order('ctime asc,id')->select();
        //获取每条评论的用户信息及回复信息
        foreach ($log_comment_list as $k => $value) {
            $uid = $value['uid'];
            $user = $user_model->find($uid);
            $log_comment_list[$k]['user'] = $user;
            //获取每条评论回复列表
            $log_hflist = $log_comment_hfmodel->where(array('com_id'=>$value['id']))->select();
            //获取每条回复用户信息
            foreach ($log_hflist as $k1 => $v1) {
                $user1 = $user_model->find($v1['uid']);
                $user2 = $user_model->find($v1['hf_id']);
                $log_hflist[$k1]['user1'] = $user1;
                $log_hflist[$k1]['user2'] = $user2;
            }
            $log_comment_list[$k]['hflist'] = $log_hflist;
        }
        $log_list[$key]['photo'] = '';
        $log_list[$key]['goodnum'] = $log_good_num;
        $log_list[$key]['comnum'] = $log_comment_num;
        $log_list[$key]['comment'] = $log_comment_list;
        $log_list[$key]['type'] = 'Log';
    }

    //获取好友相册动态列表
    $album_list = $album_model->field(array('id','uid','album_name'=>'title','album_describe'=>'content','ctime'))->where($where)->select();
    foreach ($album_list as $key => $val) {
        $id = $val['id'];
        $uid = $val['uid'];
        //获取每个相册的用户信息
        $user = $user_model->find($uid);
        $album_list[$key]['user'] = $user;
        //获取每个相册的点赞数量
        // $album_good_num = $album_good_model->where(array('album_id'=>$id))->count();
        //获取每个相册的评论数量
        // $album_comment_num = $album_comment_model->where(array('who_id'=>$id))->count();
        //获取每个相册的照片列表
        $album_photo_list = $album_photo_model->where(array('album_id'=>$id))->order('ctime desc,id')->select();
        foreach ($album_photo_list as $k => $v) {
            $album_photo_list[$k]['path'] = substr($v['path'],8);
        }
        //获取每个相册的评论列表
        // $album_comment_list = $album_comment_model->where(array('who_id'=>$id))->order('ctime asc,id')->select();
        //获取每条评论的用户信息及回复信息
        /*foreach ($album_comment_list as $k => $value) {
            $uid = $value['uid'];
            $user = $user_model->find($uid);
            $album_comment_list[$k]['user'] = $user;
            //获取每条评论回复列表
            $album_hflist = $album_comment_hfmodel->where(array('com_id'=>$value['id']))->select();
            //获取每条回复用户信息
            foreach ($album_hflist as $k1 => $v1) {
                $user1 = $user_model->find($v1['uid']);
                $user2 = $user_model->find($v1['hf_id']);
                $album_hflist[$k1]['user1'] = $user1;
                $album_hflist[$k1]['user2'] = $user2;
            }
            $album_comment_list[$k]['hflist'] = $album_hflist;
        }*/
        $album_list[$key]['photo'] = $album_photo_list;
        // $album_list[$key]['goodnum'] = $album_good_num;
        $album_list[$key]['goodnum'] = 0;
        // $album_list[$key]['comnum'] = $album_comment_num;
        $album_list[$key]['comnum'] = 0;
        // $album_list[$key]['comment'] = $album_comment_list;
        $album_list[$key]['comment'] = '';
        $album_list[$key]['type'] = 'Album';
    }

    //合并动态列表
    $arr = array_merge($say_list,$log_list,$album_list);
    // dump($arr);
    $uid = $ctime = array();
    foreach($arr as $key => $value){
        $uid[$key] = $value['uid'];
        $ctime[$key] = $value['ctime'];
    }       
    //对动态列表按时间排序
    array_multisort($ctime, SORT_DESC, $uid, SORT_ASC, $arr);

    //获取我去看过谁，谁来看过我
    $vi_mdl = D('visitor');
    $whocome = $vi_mdl->getvisitor($_SESSION['qzone']['home']['id']);
    $seewho = $vi_mdl->seewho($_SESSION['qzone']['home']['id']);

    $homeuser = $user_model->find($_SESSION['qzone']['home']['id']);//查询当前用户信息

    $week = date('N',time());
    switch ($week) {
        case '1':
            $week = '星期一';
            break;
        case '2':
            $week = '星期二';
            break;
        case '3':
            $week = '星期三';
            break;
        case '4':
            $week = '星期四';
            break;
        case '5':
            $week = '星期五';
            break;
        case '6':
            $week = '星期六';
            break;
        case '7':
            $week = '星期天';
            break;
                    
        default:
            
            break;
    }

    // 获取天气信息
    $weather = $this->getWeather($_SESSION['qzone']['home']['id']);
    $this->assign('weather',$weather);

    // 获取签到日期
    $sign_model = M('sign');
    $num = $sign_model->where(array('uid'=>$_SESSION['qzone']['home']['id']))->count();
    $this->assign('signday',$num);
    $this->assign('list',$arr);//赋值动态列表输出
    $this->assign('user',$homeuser);// 赋值当前用户信息输出
    $this->assign('vuser',$homeuser);// 赋值当前用户信息输出
    $this->assign('whocome',$whocome);
    $this->assign('seewho',$seewho);
    $this->assign('week',$week);
    $this->display();
    }

    public function getWeather($id){
        $user_model = M('user');
        $residence = $user_model->field('residence')->find($id);
        if($residence['residence']){
            $city = $residence['residence'];
        } else {
            $city = '上海';
        }

        

        $url = 'http://api.map.baidu.com/telematics/v3/weather?location='.$city.'&output=json&ak=vqMyoRRwVgEq3tU1Wr6i0ij5';
        $api = new \Common\Util\ResultApi();
        $data = $api->getApiData($url, 'json');
        $data = $data->results[0];
        $data = $data->weather_data;
        $rtn['dayPictureUrl'] = $data[0]->dayPictureUrl;
        $rtn['nightPictureUrl'] = $data[0]->nightPictureUrl;
        $rtn['weather'] = $data[0]->weather;
        $rtn['wind'] = $data[0]->wind;
        $rtn['temperature'] = $data[0]->temperature;
        $rtn['city'] = $city;
        return $rtn;
    }
    public function sign(){
        if (IS_AJAX) {
            $score = 10;
            $sign_model = M('sign');
            $uid = $_SESSION['qzone']['home']['id'];
            $now = time();
            $nowdate = date("Y-m-d",$now);

            $sign = $sign_model->where(array('uid'=>$uid))->order('ctime desc')->find();
            $num = $sign_model->where(array('uid'=>$uid))->count();
            $last_sign = $sign['ctime'];
            $last_sign = date("Y-m-d",$last_sign);
            // dump($sign);dump($nowdate==$last_sign);exit;
            if(!empty($sign) && ($nowdate==$last_sign)){
                $data['status'] = 'failed';
                $data['msg'] = '您今天已经签到了';
                $data['num'] = $sign_model->where(array('uid'=>$uid))->count();
                $this->ajaxReturn($data);
            } else {
                $sdata['uid'] = $uid;
                $sdata['ctime'] = $now;
                $sdata['score'] = $score;
                $res = $sign_model->add($sdata);

                if ($res) {
                    $data['status'] = 'success';
                    $data['msg'] = '签到成功';
                    $data['num'] = $num+1;
                    $this->ajaxReturn($data);
                } else {
                    $data['status'] = 'failed';
                    $data['msg'] = '签到失败';
                    $data['num'] = $num;
                    $this->ajaxReturn($data);
                }

            }

        } else {
            $this->error("非法请求");
        }
    }

}
